package com.example.demo.service;

import com.example.demo.dao.workorderMapper;
import com.example.demo.pojo.Workorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WorkorderService {
    @Autowired
    workorderMapper workordermapper;

    @Transactional(timeout = 10, readOnly = true, rollbackFor = Exception.class)
    public List<Workorder> selectAll(){
        return workordermapper.selectAll();
    }

    @Transactional(timeout = 10, readOnly = true, rollbackFor = Exception.class)
    public Workorder selectById(int order_id) {
        return workordermapper.selectById(order_id);
    }

    @Transactional(timeout = 10, rollbackFor = Exception.class)
    public List<Workorder> selectBybusId(int id){
        return workordermapper.selectBybusId(id);
    }

    @Transactional(timeout = 10, rollbackFor = Exception.class)
    public void addWorkorder(Workorder workorder){
        workordermapper.addWorkorder(workorder);
    }

    @Transactional(timeout = 10, rollbackFor = Exception.class)
    public void updateWorkorder(Workorder workorder){
        workordermapper.updateWorkorder(workorder);
    }

    @Transactional(timeout = 10, rollbackFor = Exception.class)
    public void deleteWorkorder(int id){
        workordermapper.deleteWorkorder(id);
    }

    @Transactional(timeout = 10, rollbackFor = Exception.class)
    public void changeAllWorkorder(List<Workorder> workorders){
        for(Workorder workorder: workorders){
            workordermapper.updateWorkorder(workorder);
        }
    }

    @Transactional(timeout = 10, readOnly = true, rollbackFor = Exception.class)
    public int order_num(){
        return workordermapper.order_num();
    }

    @Transactional(rollbackFor = Exception.class)
    public void update_performance(){
        List<Workorder> workorders = workordermapper.get_staffmoney();
        List<Workorder> allworkorders = workordermapper.selectAll();
        for(Workorder workorder: allworkorders){
            int temp_id = workorder.getStaff_id();
            workordermapper.reset_performance(temp_id);
        }
        for(Workorder workorder: workorders){
            System.out.println(workorder);
            int staff_id = workorder.getStaff_id();
            int money = workorder.getTotal_money();
            workordermapper.reset_performance(staff_id);
            workordermapper.update_performance(staff_id,money);
        }
    }
}
